﻿using GDGeek;
using MrPP.UX;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet { 
    public class SecondScreen : MonoBehaviour
    {
        [SerializeField]
        private SearchRoot _search;
        public SearchRoot search
        {
            get
            {
                return _search;
            }
        }
        [SerializeField]
        private ShelveRoot _shelve;
        public ShelveRoot shelve
        {
            get
            {
                return _shelve;
            }
        }

        [SerializeField]
        private CheckRoot _check;
        public CheckRoot check {
            get {
                return _check;
            }
        }

        [SerializeField]
        RotateUIEffect _runner;
        [SerializeField]
        int isOpen_ = -1;

        public int isOpen {
            get {
                return isOpen_;
            }
        }
        [SerializeField]
        int targetOpen_ = -1;
        void  doOpen(int last, int next) {


          //  Debug.LogError("change open" + next);
            if (isOpen_ != next) {
                targetOpen_ = next;
                refresh();
            }
        }
        bool busy = false;

        public GameObject getWindow(int i) {
            if (i == 0) {
                return search.gameObject;
            }
            else if (i == 1)
            {
                return check.gameObject;
            }
            else if (i == 2)
            {
                return shelve.gameObject;
            }
            return null;
        }

        private void refresh()
        {
            
            if (isOpen_ != targetOpen_ && !busy) {
                TaskList tl = new TaskList();
                if (isOpen_ != -1) {
                    GameObject old = getWindow(isOpen_);
                    if (old != null) {
                        Task hide = this._runner.hide();
                        TaskManager.PushBack(hide, delegate
                        {
                            ButtonController btc = old.GetComponent<ButtonController>();
                            if (btc!=null)
                            {
                                btc._button.state = Button.State.Enabled;
                            }
                            old.SetActive(false);

                        });
                        tl.push(hide);
                    }

                    
                }

                if (targetOpen_ != -1)
                {
                    GameObject curr = getWindow(targetOpen_);
                    if (curr != null)
                    {
                        Task show = this._runner.show();
                        TaskManager.PushFront(show, delegate
                        {
                            ButtonController btc = curr.GetComponent<ButtonController>();
                            if (btc != null)
                            {
                                btc._button.state = Button.State.Selected;
                            }
                            curr.SetActive(true);

                        });
                        tl.push(show);
                    }
                }
                TaskManager.PushFront(tl, delegate
                {
                    isOpen_ = targetOpen_;
                    busy = true;
                });


                TaskManager.PushBack(tl, delegate
                {
                    busy = false;
                    refresh();
                });
                TaskManager.Run(tl);
            }
        }

        
        void Start()
        {
            Model.Instance.search.onOpen += doOpen;
            doOpen(-1, -1);
            
      
        }
     


    }
}
